package com.swpu.service_comsumer.utils;

import com.alibaba.fastjson.JSON;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;
import groovy.util.logging.Log4j;
import groovy.util.logging.Slf4j;
import lombok.extern.java.Log;

import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;
import java.util.concurrent.ExecutionException;
@Slf4j
public class SmsSender {
    public static String ACCESS_KEY_ID="LTAI5t8gieNpFb8ECLrCLjFb";
    public static String SECRET="Fnk6S9KJgbzEv27IIFREVBzCzCNdtD";
    public static void sendSms(String phoneNumber, String remindType,String dateStr) {
        System.out.println("开始发送短信");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//固定值
        request.setSysVersion("2017-05-25");//固定值
        request.setSysAction("SendSms");//选择SendSms API
        request.putQueryParameter("RegionId", "cn-hangzhou");//固定值
        request.putQueryParameter("SignName", "悠悠云上健康");//签名
        request.putQueryParameter("TemplateCode", "SMS_465321902");//模板
        request.putQueryParameter("PhoneNumbers", phoneNumber);//手机号
        request.putQueryParameter("TemplateParam", "{\"type\":\""+remindType+"\",\"time\":\""+dateStr+"\"}");//模板中json格式的code参数，随机码

        try {
            CommonResponse response = client.getCommonResponse(request);
            HttpResponse httpResponse = response.getHttpResponse();
            int status = httpResponse.getStatus();
            System.out.println("短信发送完成");
            if(status!=200){

                throw new RuntimeException("短信发送失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("短信发送失败");
        }
    }

}

